package com.yykj.pay.server.controller;

import com.github.wxpay.sdk.WXPayUtil;
import com.yykj.common.vo.ResultVO;
import com.yykj.pay.center.client.form.PayCreateForm;
import com.yykj.pay.server.service.PaymentService;
import com.yykj.pay.server.util.PayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;


@RestController
@RequestMapping("/pay")
@Api(tags = "支付")
@Slf4j
public class PaymentController {

    @Autowired
    PaymentService paymentService;

    @PostMapping("/create")
    @ApiOperation(value = "微信小程序下单,返回支微信付信息")
    public ResultVO<Map<String, String>> create(@RequestBody @Valid PayCreateForm payCreateForm) {
        return ResultVO.success(paymentService.create(payCreateForm));
    }

    @ApiIgnore
    @RequestMapping(value = "/callback/{orderNo}")
    public void callback(@PathVariable("orderNo") String orderNo) {
        log.info("微信支付回调信息 => {}", orderNo);
       try {
           ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
           HttpServletRequest request = attributes.getRequest();
           HttpServletResponse response = attributes.getResponse();
           String xml = PayUtil.convertToString(request.getInputStream());
            log.info("微信支付回调信息 => {}", xml);
            Map<String, String> map = WXPayUtil.xmlToMap(xml);
            if ("SUCCESS".equals(map.get("result_code"))) {
                response.setContentType("text/xml");
                String data = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                response.getWriter().write(data);
                paymentService.callback(orderNo);
            }
        } catch (Exception e) {
           log.info("微信支付回调错误 => {}",e );
            e.printStackTrace();
        }
    }

    @ApiIgnore
    @RequestMapping(value = "/refund/callback/{orderNo}")
    public void refundCallback(@PathVariable("orderNo") String orderNo) {
        log.info("微信退款回调信息 => {}", orderNo);
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            HttpServletResponse response = attributes.getResponse();
            String xml = PayUtil.convertToString(request.getInputStream());
            log.info("微信退款回调信息 => {}", xml);
            Map<String, String> map = WXPayUtil.xmlToMap(xml);
            if ("SUCCESS".equals(map.get("result_code"))) {
                response.setContentType("text/xml");
                String data = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                response.getWriter().write(data);
//                paymentService.callback(orderNo);
            }
        } catch (Exception e) {
            log.info("微信退款回调信息 => {}",e );
            e.printStackTrace();
        }
    }

    @GetMapping("/testMq/{orderNo}")
    @ApiOperation(value = "mq测试")
    public ResultVO testMq(@PathVariable("orderNo") String orderNo) {
        paymentService.testMq(orderNo);
        return ResultVO.success();
    }


}